package com.xiongtian.linkedlist;

/**
 * @author xiongtian
 * @version 1.0
 * @date 2021/3/6 16:14
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {

        System.out.println("双向链表的测试：");
        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨~~~~");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟~~~");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星~~~");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头~~~");
        DoubleLinkedList list = new DoubleLinkedList();
        list.add(heroNode1);
        list.add(heroNode2);
        list.add(heroNode3);
        list.add(heroNode4);
        list.list2();


        System.out.println("测试更新：");
        HeroNode2 heroNode5 = new HeroNode2(2, "小卢", "小麒麟~~~");
        list.updateNode(heroNode5);
        list.list();

        System.out.println("测试删除: ");
        list.del(4);
        list.list();

        System.out.println("测试按照顺序插入： >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        DoubleLinkedList list2 = new DoubleLinkedList();
        HeroNode2 heroNode6 = new HeroNode2(1, "宋江", "及时雨~~~~");
        HeroNode2 heroNode7 = new HeroNode2(2, "卢俊义", "玉麒麟~~~");
        HeroNode2 heroNode8 = new HeroNode2(3, "吴用", "智多星~~~");
        HeroNode2 heroNode9 = new HeroNode2(4, "林冲", "豹子头~~~");

        list2.addByOrder(heroNode9);
        list2.addByOrder(heroNode6);
        list2.addByOrder(heroNode7);
        list2.addByOrder(heroNode8);
        list2.list();
    }
}

// 创建一个双向链表的类
class DoubleLinkedList {
    // 初始化一个头节点，头节点先不要动，不存放具体的数据
    private HeroNode2 head = new HeroNode2(0, "", "");

    // 返回头及诶单
    public HeroNode2 getHead() {
        return head;
    }

    // 遍历双向链表的方法
    public void list() {
        if (head.next == null) {
            return;
        }
        HeroNode2 cur = head.next;
        while (cur != null) {
            System.out.println(cur.toString());
            cur = cur.next;
        }
    }

    // 注意区别
    /*  HeroNode2 cur = head.next; 决定的是从那个开始
        而 while (cur.next != null) 和 while (cur != null) 决定的是结束时指针的位置
        前者是  cur指向的是最有一个不为空的节点且该节点没有执行循环里面的代码,
        而后者结束时是  cur指向的是最后一个不为空的节点的后一个节点，最有一个不为空的节点执行了循环里面的代码
    */
    public void list2() {
        if (head.next == null) {
            return;
        }
        HeroNode2 cur = head.next;
        while (cur.next != null) {
            System.out.println(cur.toString());
            cur = cur.next;
        }

        System.out.println(cur.toString());
    }

    // 添加一个节点到双向链表的最后
    public void add(HeroNode2 newNode) {
        HeroNode2 cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
        newNode.pre = cur;
    }

    /**
     * 顺序添加
     *
     * @param newNode
     */
    public void addByOrder(HeroNode2 newNode) {

        HeroNode2 cur = head;
        boolean flag = false;
        while (cur.next != null) {
            if (newNode.no < cur.next.no) {
                break;
            } else if (newNode.no == cur.next.no) {
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if (flag) {
            System.out.println("已经存在添加失败");
        } else {
            newNode.next = cur.next;
            cur.next = newNode;
            newNode.pre = cur;
            if (cur.next != null) {
                cur.next.pre = newNode;
            }

        }
    }

    // 从双向链表中修改节点
    public void updateNode(HeroNode2 node) {
        if (head.next == null) {
            System.out.println("链表为空违法更新");
            return;
        }
        HeroNode2 cur = head.next;
        boolean flag = false;
        while (cur != null) {
            if (cur.no == node.no) {
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if (flag) {
            cur.name = node.name;
            cur.nickName = node.nickName;
        } else {
            System.out.println("没有找到 no：" + node.no + "对应的元素！");
        }
    }

    // 从双向链表中删除节点
    public void del(int no) {
        if (head.next == null) {
            System.out.println("链表为空无法删除！");
            return;
        }
        HeroNode2 cur = head.next;
        boolean flag = false;
        while (cur != null) {
            if (cur.no == no) {
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if (flag) {
            cur.pre.next = cur.next;
            // 这里是有风险的（代码有问题）
            // 如果是最后一个节点就不需要执行下面这句话，否则会出现空指针异常
            if (cur.next != null) {
                cur.next.pre = cur.pre;
            }
        } else {
            System.out.println("没有找到节点！");
        }

    }
}

// 定义一个heroNode，每个HeroNode 对象就是一个节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;

    public HeroNode2 next; // 指向下一个节点，默认为null
    public HeroNode2 pre;  // 指向前一个节点，默认为null

    public HeroNode2() {
    }

    // 构造器
    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    // 为了显示方便重写同String方法
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}